# -*- coding: utf-8 -*-
"""Untitled4.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1Ias7HGqmNJiXMhI9Xt3RHPn-y9TUIZzK
"""

'''
Felhasznált források:
https://www.digitalocean.com/community/tutorials/how-to-build-a-neural-network-to-recognize-handwritten-digits-with-tensorflow
'''


import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('/tmp/mnist', one_hot=True)

IMG_SIZE = 28

input_size = IMG_SIZE * IMG_SIZE
hidden1_size = 512 
hidden2_size = 256
hidden3_size = 128
output_size = 10
learning_rate = 1e-4
batch_size = 128
n_iterations = 10000

loss_list = list()
accuracy_list = list()

tf.reset_default_graph() 

input_img = tf.placeholder(tf.float32, [None, input_size])
input_labels = tf.placeholder(tf.float32, [None, output_size])


with tf.variable_scope('hidden1'):
  weights = tf.get_variable('weights', [input_size, hidden1_size])
  bias = tf.Variable(tf.constant(0.1, shape=[hidden1_size]))
  be = tf.matmul(input_img, weights) + bias
  y1 = tf.nn.relu(be)
  
  
with tf.variable_scope('hidden2'):
  weights = tf.get_variable('weights', [hidden1_size, hidden2_size])
  bias = tf.Variable(tf.constant(0.1, shape=[hidden2_size]))
  be = tf.matmul(y1, weights) + bias
  y2 = tf.nn.relu(be)
  
with tf.variable_scope('hidden3'):
  weights = tf.get_variable('weights', [hidden2_size, hidden3_size])
  bias = tf.Variable(tf.constant(0.1, shape=[hidden3_size]))
  be = tf.matmul(y2, weights) + bias
  y3 = tf.nn.relu(be)
  
with tf.variable_scope('output'):
  weights = tf.get_variable('weights', [hidden3_size, output_size])
  bias = tf.Variable(tf.constant(0.1, shape=[output_size]))
  be = tf.matmul(y3, weights) + bias
  y = be
  
loss = tf.reduce_mean(tf.losses.softmax_cross_entropy(input_labels, y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

correct_pred = tf.equal(tf.argmax(y, 1), tf.argmax(input_labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())  

  for i in range(n_iterations):
    
    batch_x, batch_y = mnist.train.next_batch(batch_size)
    _, l, v = sess.run([optimizer, loss, accuracy], feed_dict={input_img : batch_x, input_labels: batch_y})
    loss_list.append(l)
    accuracy_list.append(v)
    if i % 500 == 0:
      print('loss=', l, 'accu=', v, 'step=', i)
    
plt.plot(loss_list)
plt.plot(accuracy_list)
  
#m = np.reshape(mnist.train.images[1,:],(28,28))



